//
//  ShopTypeDetailViewController.m
//  EPBPro
//
//  Created by wucha on 2018/6/27.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "ShopTypeDetailViewController.h"
#import "ResizeView.h"
#import "HttpContractAction.h"
#import "ContractEquipmentListViewController.h"

@interface ShopTypeDetailViewController (){
    ShopTypeDetailBlock shopTypeDetailBlock;
}
@property (nonatomic,strong) MerchantContractInfo *merchantContractInfo;

@end

@implementation ShopTypeDetailViewController

-(id)initWithShopTypeInfo:(MerchantContractInfo*)merchantContractInfo Block:(ShopTypeDetailBlock)block{
    if (self == [super init]) {
        self.merchantContractInfo = merchantContractInfo;
        shopTypeDetailBlock = block;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"合同详情";
    ResizeView *resizeView = [[ResizeView alloc] initWithFrame:CGRectMake(0, StatusBarHeight+44, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarHeight-44)];
    [self.view addSubview:resizeView];
    
    [resizeView addResizeSubview:[self getSpaceResizeViewInfo:10]];
    [resizeView addResizeSubview:[self getNormalResizeViewInfo:@"合同编号" value:self.merchantContractInfo.id]];
    [resizeView addResizeSubview:[self getNormalResizeViewInfo:@"甲方公司" value:self.merchantContractInfo.jiaOrgName]];
    [resizeView addResizeSubview:[self getNormalResizeViewInfo:@"已方公司" value:self.merchantContractInfo.yiOrgName]];
    [resizeView addResizeSubview:[self getNormalResizeViewInfo:@"有效期" value:[NSString stringWithFormat:@"%@到%@",[Utils dateChineseToString:[Utils stringToDate:self.merchantContractInfo.startDate]],[Utils dateChineseToString:[Utils stringToDate:self.merchantContractInfo.endDate]]]]];
    
    [resizeView addResizeSubview:[self getNormalResizeViewInfo:@"金额" value:self.merchantContractInfo.amount]];
    [resizeView addResizeSubview:[self getNormalResizeViewInfo:@"机器数量" value:self.merchantContractInfo.equipmentNum]];
    [resizeView addResizeSubview:[self getEquipmentListResizeViewInfo:@"设备列表"]];
    [resizeView addResizeSubview:[self getNormalResizeViewInfo:@"落地返利" value:self.merchantContractInfo.returnAmount]];
    [resizeView addResizeSubview:[self getNormalResizeViewInfo:@"利润占比" value:self.merchantContractInfo.benifitRate]];
    NSString *area = [NSString stringWithFormat:@"%@%@%@",self.merchantContractInfo.province,self.merchantContractInfo.city,self.merchantContractInfo.area];
    [resizeView addResizeSubview:[self getNormalResizeViewInfo:@"代理区域" value:area]];
    [resizeView addResizeSubview:[self getNormalResizeViewInfo:@"备注" value:@""]];
    [resizeView addResizeSubview:[self getMarkResizeViewInfo:self.merchantContractInfo.descn]];
    if ([self.merchantContractInfo.status isEqualToString:@"drafting"]) {
        if ([Utils getUserPermission:@"contractreview"]) {
            [resizeView addResizeSubview:[self getSubmitResizeViewInfo]];

        }
    }else if ([self.merchantContractInfo.status isEqualToString:@"confirmed"]){
         if ([Utils getUserPermission:@"contractreview"]) {
             [resizeView addResizeSubview:[self getCancelResizeViewInfo]];

         }

    }

}

-(void)submitAction:(id)sender{
    [BlockAlertView showAlertWithTitle:@"确认合同?" message:@"" alertViewStyle:UIAlertViewStyleDefault block:^(id clickView, NSInteger index) {
        if (index == 1) {
            [self showWaitingViewWithMessage:@"确认中..." style:WaitingViewShowStyleFullSelfView];
            [HttpContractAction postConfirmContract:self.merchantContractInfo.id block:^(id result, NSError *error) {
                [self dismissWaitingView];
                if (!error) {
                    [self showMessage:@"合同确认成功" autoDissmissAfterDelay:0];
                    if (shopTypeDetailBlock) {
                        shopTypeDetailBlock();
                    }
                    [self performBlock:^{
                        [self.navigationController popViewControllerAnimated:YES];
                    } afterDelay:0.8];
                }else{
                    [self showMessage:error.domain autoDissmissAfterDelay:0];
                }
            }];
        }
    } cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    
}

-(void)cancelAction:(id)sender{
    [BlockAlertView showAlertWithTitle:@"确认终止合同?" message:@"" alertViewStyle:UIAlertViewStyleDefault block:^(id clickView, NSInteger index) {
        if (index == 1) {
            [self showWaitingViewWithMessage:@"终止中..." style:WaitingViewShowStyleFullSelfView];
            [HttpContractAction postTerminateContract:self.merchantContractInfo.id block:^(id result, NSError *error) {
                [self dismissWaitingView];
                if (!error) {
                    [self showMessage:@"合同终止成功" autoDissmissAfterDelay:0];
                    if (shopTypeDetailBlock) {
                        shopTypeDetailBlock();
                    }
                    [self performBlock:^{
                        [self.navigationController popViewControllerAnimated:YES];
                    } afterDelay:0.8];
                }else{
                    [self showMessage:error.domain autoDissmissAfterDelay:0];
                }
                
            }];
        }
    } cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    
}

-(ResizeViewInfo*)getCancelResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 60;
    
    UIButton *submitButton = [UIButton new];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [submitButton setTitle:@"终止合同" forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitButton.backgroundColor = DefaultTenantColor;
    [resizeViewInfo addSubview:submitButton];
    submitButton.sd_layout.leftSpaceToView(resizeViewInfo.view, 40).topSpaceToView(resizeViewInfo.view, 20).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(SCREEN_WIDTH-80);
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getSubmitResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 60;
    
    UIButton *submitButton = [UIButton new];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [submitButton setTitle:@"确认合同" forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitButton.backgroundColor = DefaultTenantColor;
    [resizeViewInfo addSubview:submitButton];
    submitButton.sd_layout.leftSpaceToView(resizeViewInfo.view, 40).topSpaceToView(resizeViewInfo.view, 20).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(SCREEN_WIDTH-80);
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getEquipmentListResizeViewInfo:(NSString*)title{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(100);
    
    UIImageView *arrowImageView = [UIImageView new];
    arrowImageView.image = [UIImage imageNamed:@"arrow_right"];
    arrowImageView.contentMode = UIViewContentModeCenter;
    [resizeViewInfo addSubview:arrowImageView];
    arrowImageView.sd_layout.rightSpaceToView(resizeViewInfo.view, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(20);
    
    UIButton *button = [UIButton new];
    [button addTarget:self action:@selector(equipmentListAction:) forControlEvents:UIControlEventTouchUpInside];
    [resizeViewInfo addSubview:button];
    button.sd_layout.leftSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 0).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0);
    
    return resizeViewInfo;
}

-(void)equipmentListAction:(id)sender{
    ContractEquipmentListViewController *contractEquipmentListViewController = [[ContractEquipmentListViewController alloc] initWithContractId:self.merchantContractInfo.id];
    [self.navigationController pushViewController:contractEquipmentListViewController animated:YES];
}

-(ResizeViewInfo*)getMarkResizeViewInfo:(NSString*)mark{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 100;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UITextView *titleTextView = [UITextView new];
    titleTextView.text = mark;
    titleTextView.userInteractionEnabled = NO;
    titleTextView.textColor = [UIColor grayColor];
    titleTextView.font = [UIFont systemFontOfSize:14];
    [resizeViewInfo addSubview:titleTextView];
    titleTextView.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(SCREEN_WIDTH-30);
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getNormalResizeViewInfo:(NSString*)title value:(NSString*)valueString{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(100);
    
    UILabel *nameLabel = [UILabel new];
    nameLabel.text = valueString;
    nameLabel.textColor = [UIColor grayColor];
    nameLabel.font = [UIFont systemFontOfSize:16];
    nameLabel.textAlignment = NSTextAlignmentRight;
    [resizeViewInfo addSubview:nameLabel];
    nameLabel.sd_layout.rightSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [nameLabel setSingleLineAutoResizeWithMaxWidth:300];
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getSpaceResizeViewInfo:(int)height{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = height;
    
    return resizeViewInfo;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
